#!/bin/bash
#set -x

wd=$(pwd)

flist=package.flist

if [ -f ${flist} ]
then

 for jarfn in $(egrep -v '^#' ${flist})
 do
  jarfn_base=$(echo ${jarfn} | sed 's%\.jar$%%')
  name_base=$(echo ${jarfn_base} | sed 's/sdl-natives-//')
  name_os=$(echo ${name_base} | sed 's/-.*//')
  name_arch=$(echo ${name_base} | sed 's/.*-//')
  natives_dir=${name_os}/${name_arch}

  target_jar=${wd}/../web/${jarfn}

  if [ -d "${natives_dir}" ]&&[ -n "$(2>/dev/null ls -d ${natives_dir}/* )" ]
  then
   if cd "${natives_dir}"
   then
     cat<<EOF>&2
Packing: '${jarfn}' from '${natives_dir}'...
EOF
     if jar cvf ${target_jar} * 
     then
       ls -l ${target_jar}
     else
      rm -f ${target_jar}
      cat<<EOF>&2
Skipping: '${jarfn}' from '${natives_dir}', error creating jar.
EOF
     fi
     cd ${wd}
   else
    cat<<EOF>&2
${0}: error changing directory into '${natives_dir}'.
EOF
    exit 1
   fi
  else
   rm -f ${target_jar}
   mkdir -p ${natives_dir}
   cat<<EOF>&2
Skipping: '${jarfn}', directory not found or empty '${natives_dir}'.
EOF
  fi
 done
else
 cat<<EOF&2
${0}: error missing configuration file '${flist}'.
EOF
 exit 1
fi
